/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#ifndef NODE_UTIL_BYTEBUFFER_H_
#define NODE_UTIL_BYTEBUFFER_H_

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif

#include <cstddef>
#include <cstdint>
#include <vector>

#include "node/util/bytebuffer_exception.h"

namespace node {
    namespace util {

        class ByteBuffer
        {
        public:
            const static ::std::size_t DEFAULT_SIZE = 0x1000; // 4K
            const static ::std::size_t MAX_SIZE = 0x100000; // (1M)

            typedef ByteBuffer  Self;

        public:
            ByteBuffer(void)
                : rpos_(0)
                , wpos_(0)
                , storage_()
            {
                storage_.reserve(DEFAULT_SIZE);
            }

            explicit ByteBuffer(::std::size_t size)
                : rpos_(0)
                , wpos_(0)
                , storage_(size)
            {

            }

            ByteBuffer(const ByteBuffer &buf)
                : rpos_(buf.rpos_)
                , wpos_(buf.wpos_)
                , storage_(buf.storage_)
            {

            }

            virtual ~ByteBuffer(void)
            {

            }

            void Clear()
            {
                storage_.clear();
                rpos_ = wpos_ = 0;
            }

            void Resize(::std::size_t newsize)
            {
                storage_.resize(newsize);
                rpos_ = 0;
                wpos_ = size();
            }

        public:
            ::std::vector<::std::uint8_t>::size_type size(void) const
            {
                return storage_.size();
            }

            const ::std::uint8_t* contents(void) const
            {
                return &storage_[0];
            }

			::std::uint8_t* wptr(::std::size_t offset)
			{
				return &storage_[offset];
			}

            Self& operator<<(::std::uint8_t x)
            {
                Append<::std::uint8_t>(x);
                return *this;
            }

            Self& operator<<(::std::uint16_t x)
            {
                Append<::std::uint16_t>(x);
                return *this;
            }

            Self& operator<<(::std::uint32_t x)
            {
                Append<::std::uint32_t>(x);
                return *this;
            }

            Self& operator<<(::std::uint64_t x)
            {
                Append<::std::uint64_t>(x);
                return *this;
            }

            Self& operator<<(float x)
            {
                Append(x);
                return *this;
            }

            Self& operator<<(const char* x)
            {
                Append(x, strlen(x));
                *this << (::std::uint8_t)0;
                return *this;
            }

            Self& operator<<(const ::std::string& x)
            {
                Append(x.c_str(), x.length());
                *this << (::std::uint8_t)0;
                return *this;
            }

            void Append(const char* src, ::std::size_t len)
            {
                if (len == 0)
                    return;

                if ((len + wpos_) > MAX_SIZE)
                    throw ::node::util::ByteBufferException(true, wpos_, storage_.size(), len);

                if (storage_.size() < (len + wpos_))
                    storage_.resize(len + wpos_);

                memcpy(&storage_[wpos_], src, len);
                wpos_ += len;
            }

            void Append(const ::std::uint8_t* src, ::std::size_t len)
            {
                Append((const char*)src, len);
            }

            void Append(const ByteBuffer& bbuf)
            {
                Append(bbuf.contents(), bbuf.size());
            }

        protected:
            template<typename T>
            void Append(T x)
            {
                Append((const char*)&x, sizeof(T));
            }

        protected:
            ::std::size_t                 rpos_;
            ::std::size_t                 wpos_;
            ::std::vector<::std::uint8_t>   storage_;
        };

    }
}

#endif // NODE_UTIL_BYTEBUFFER_H_
